package com.xiaolin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AppService {

    private Integer thisAid = 1;

    private Integer thisPage = 1;

    private Integer sumPage;

    public synchronized Integer getPage() {
        if(thisPage.equals(sumPage)){
            System.out.println("写入完毕!");
            System.exit(0);
        }
        return ++thisPage;
    }

    /**
     * 获取Id
     *
     * @return
     */
    public synchronized Integer getId() {
        return ++thisAid;
    }

    @Autowired
    private VideoSerivce videoSerivce;

//    @Autowired
//    private HttpService httpService;

    @Autowired
    private AppService appService;


    /**
     * 爬虫开始
     */
    public void start() {
        Integer max = videoSerivce.getMax();
        this.thisAid = max;

        System.out.println("当前数据库最大值ID为:" + max);

        System.out.println("正在开始多线程爬虫");

        for (int i = 1; i <= 8; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        while (true) {
                            Integer id = appService.getId();
                            try {
//                                httpService.start(id);
//                                Thread.sleep(100);
                            } catch (Exception e) {
                                System.out.println("=============> 报错时间:" + new Date().toLocaleString() + "  " + e.getMessage());
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("===> " + getName() + " 线程被莫名停止 info:");
                        System.out.println(e.getMessage());
                    }
                }
            };
            thread.start();
            System.out.println("线程" + i + "已经启动!");
        }


    }


    public void startES() {
        sumPage = videoSerivce.getSumPage();
        System.out.println("总页数: " + sumPage + " --- 当前页: " + thisPage);

        for (int i = 1; i <= 8; i++) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        while (true) {
                            Integer page = appService.getPage();
                            System.out.println(getName() + "准备写入第: " + page + "页");
                            try {
                                videoSerivce.save(page);
                                System.out.println(getName() + "完成写入第: " + page + "页");
                            } catch (Exception e) {
                                System.out.println("=============> 报错时间:" + new Date().toLocaleString() + ":  " + e.getMessage());
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("===> " + getName() + " 被莫名停止 info:" + e.getMessage());
                    }
                }
            };
            thread.setName("线程" + i);
            thread.start();
            System.out.println(thread.getName() + "已经启动!");
        }

    }


}
